package cn.com.stums.service.inbound;

import java.util.List;

import org.apache.log4j.Logger;

import cn.com.stums.dao.inbound.InboundDao;
import cn.com.stums.dao.store.StoreDao;
import cn.com.stums.model.inbound.InBoundList;
import cn.com.stums.model.inbound.InBoundListDetail;
import cn.com.stums.model.store.Store;
import cn.com.stums.util.OrderCreateUtil;

public class InboundServiceImp implements InboundService {
	private InboundDao inboDao;
	private StoreDao storeDao;
	private static Logger logger = Logger.getLogger(InboundServiceImp.class);
	
	public boolean createInboundList(InBoundList bean, List<Store> storeList) throws RuntimeException{
		boolean result = true;
		try{
			 String orderNo = (String) OrderCreateUtil.getObject();
			 bean.setStInboundAppNumber(orderNo);
			 inboDao.save(bean);
			 if(storeList !=null && storeList.size() > 0){
				 for(Store tempbean:storeList){
					 InBoundListDetail detailBean = new InBoundListDetail();
					 detailBean.setStInboundAppNumber(orderNo);
					 detailBean.setStSnNumber(tempbean.getStSnNumber());
					 storeDao.saveStore(tempbean);
				 }
			  }
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.toString()); 
			result = false;
			throw new RuntimeException(e.toString());
		}
		 return result;
	}
	
	/* (non-Javadoc)
	 * @see cn.com.stums.service.inbound.InboundService#query(int, cn.com.stums.model.inbound.InBoundList, java.util.List)
	 */
	public void query(int id,InBoundList bean,List<InBoundListDetail> list) throws RuntimeException{
		
		try{
			bean = inboDao.queryInBoundList(id);
			InBoundListDetail tempbean = new InBoundListDetail();
			tempbean.setStInboundAppNumber(bean.getStInboundAppNumber());
			
			list = inboDao.query(tempbean);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.toString()); 
			throw new RuntimeException(e.toString());
		}
	}
	
	/* (non-Javadoc)
	 * @see cn.com.stums.service.inbound.InboundService#modify(cn.com.stums.model.inbound.InBoundList, java.util.List)
	 */
	public boolean modify(InBoundList bean,List<InBoundListDetail> list) throws RuntimeException{
		boolean result = true;
		try{
			if(list !=null && list.size() > 0){
				inboDao.modifyInBoundList(bean);
				for(InBoundListDetail tempBean:list){
					inboDao.modifyInBoundListDetail( tempBean);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.toString()); 
			throw new RuntimeException(e.toString());
		}
		return result;
	}
	
	public InboundDao getInboDao() {
		return inboDao;
	}

	public void setInboDao(InboundDao inboDao) {
		this.inboDao = inboDao;
	}

	public StoreDao getStoreDao() {
		return storeDao;
	}

	public void setStoreDao(StoreDao storeDao) {
		this.storeDao = storeDao;
	}
	
}
